package com.baidu.customview.widget;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.baidu.customview.R;
import com.baidu.customview.utils.MetricsUtils;

/**
 * Created by imagine on 2015/11/14.
 */
public class WriteView extends View{
    private Paint p;
    private Bitmap bg;
    private Bitmap fg;
    private int screenW,screenH;
    private Canvas mCanvas;
    private Path path;
    private float preX,preY;
    public static final int MIN_MOVE_DIS=5;
    public WriteView(Context context) {
        super(context);
    }

    public WriteView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        p=new Paint(Paint.ANTI_ALIAS_FLAG|Paint.DITHER_FLAG);
        p.setStyle(Paint.Style.STROKE);
        p.setColor(getResources().getColor(R.color.transparent));
        p.setStrokeJoin(Paint.Join.ROUND);//设置路径结合处样式
        p.setStrokeCap(Paint.Cap.ROUND);//设置笔帽样式
        p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));//这个将图层混合，很重要
        p.setStrokeWidth(10);
        bg= BitmapFactory.decodeResource(getResources(),R.mipmap.peien);
        screenW= MetricsUtils.winMetrics(context).widthPixels;
        screenH= MetricsUtils.winMetrics(context).heightPixels;
        bg=Bitmap.createScaledBitmap(bg,screenW,screenH,true);
        fg=Bitmap.createBitmap(screenW, screenH, Bitmap.Config.ARGB_8888);
        mCanvas=new Canvas(fg);
        mCanvas.drawColor(getResources().getColor(R.color.tarmac));//初始化画布颜色
        path=new Path();
    }

    public WriteView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public WriteView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bg, 0, 0, null);
        canvas.drawBitmap(fg,0,0,null);//注意这里的前景必须与mCanvas有关联，否则将不会有任何效果
        mCanvas.drawPath(path, p);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x=event.getX();
        float y=event.getY();
        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                path.reset();
                path.moveTo(x,y);
                preX=x;
                preY=y;
                break;
            case MotionEvent.ACTION_MOVE:
                float dx=Math.abs(x-preX);
                float dy=Math.abs(y-preY);
                if(dx>MIN_MOVE_DIS||dy>MIN_MOVE_DIS){//判断移动距离是否满足绘画要求
                    path.quadTo(preX,preY,x,y);//添加路径
                    preX=x;
                    preY=y;
                }
                break;
        }
        invalidate();
        return true;
    }
     public void clear(){
         mCanvas.drawColor(getResources().getColor(R.color.tarmac));
         invalidate();
     }
}
